<?php


namespace app\admin\controller;


//use app\common\controller\Clientvalidate;
use app\common\model\System;
use app\common\validate\Vld_system;
use think\exception\ValidateException;

class Systems extends Base
{

    public  function  get_detail()
    {//获取sytem详细信息
        $id=1;
        $data = System::field(' id,state,interval_cpu,interval_mem,interval_pid1 ')
            ->where('id',$id)
            ->find();
        //返回判断
        if ($data){
            $msg='OK';
            $status=200;
        }else{
            $msg='failed';
            $status=0;
        }
        $meta=[
            'msg'=>$msg,
            'status'=>$status
        ];
        $res=[
            'data'=>$data,
            'meta'=>$meta
        ];
        return json($res);
    }

   

    public  function  edit()
    {//修改sys信息
        $data=[
            'id'=>1,
            'interval_cpu'=>input('post.interval_cpu'),
            'interval_mem'=>input('post.interval_mem'),
            'interval_pid1'=>input('post.interval_pid1'),
            'state'=>input('post.state')
        ];
        try
        {
            $validateres = validate(Vld_system::class)
                ->scene('edit')
                ->check($data);
            if( $validateres)
            {//通过验证
                $mydata = System::where('id',$data['id'])
                    ->find();
                $result= $mydata->save($data);
                if($result){
                    //判断添加是否成功
                    $msg="ok";
                    $status=200;
                }else{
                    $msg="fail";
                    $status=0;
                }
            }
        }
        catch (ValidateException  $e) {
            // 验证失败 输出错误信息
            $msg=  $e->getMessage();
            $status=0;
        }
        // 返回客户端信息
        $meta=[
            'msg'=>$msg,
            'status'=>$status
        ];
        $res=[
            'data'=>null,
            'meta'=>$meta
        ];
        return json($res);
    }
}
